<?php 

require_once("../sqlConn.php");
$action=$_GET['action'];
switch($action)
{
	case 'follow':
	   follow($_POST['friendid'],$_POST['friendname'],$_POST['followid'],$_POST['followname']);
	   break;

	case 'friendList':
	   friendList($_POST['foid']);
	   break;

	case 'followList':
	   followList($_POST['frid']);
	   break;

}

function followList($frid)
{
	connectDB();
	//echo "foid $foid" ;
	if($frid)
	{
		$sql="select foid,foname,point from friend where frid='$frid'";
		$res=mysql_query($sql);
	
		$fList="[";					
		
		while($row=mysql_fetch_array($res))
		{

			$rowStr="{foid:'".$row[0]."',foname:'".$row[1]."',point:'".$row[2]."'},";
			$fList.=$rowStr;	
		}
		if(mysql_num_rows($res)>0)
		{
			 $fList=substr_replace($fList,'',-1,1);
		}
		$fList.="]";
		echo "$fList" ;
		//return $fList;
		return;
	}
}

function friendList($foid)
{
	connectDB();
	//echo "foid $foid" ;
	if($foid)
	{
		$sql="select frid,frname,point from friend where foid='$foid'";
		$res=mysql_query($sql);
	
		$fList="[";					
		
		while($row=mysql_fetch_array($res))
		{

			$rowStr="{frid:'".$row[0]."',frname:'".$row[1]."',point:'".$row[2]."'},";
			$fList.=$rowStr;	
		}
		if(mysql_num_rows($res)>0)
		{
			 $fList=substr_replace($fList,'',-1,1);
		}
		$fList.="]";
		echo "$fList" ;
		//return $fList;
		return;
	}
}

function follow($frid,$frname,$foid,$foname)
{
	connectDB();
	//echo "frid: $frid, frname  $frname , foid $foid ,foname $foname ";
	if($frid && $frname && $foid && $foname && $frid!=$foid )
	{
		$sql="select id from friend where frid='$frid' and frname='$frname' and foid='$foid' and foname='$foname'";
		$res=mysql_query($sql);
		if(!res)
		{
			echo "他妈 不可能出现这错误";
			return;
		}
		if($row=mysql_fetch_array($res))
		{//居然已经存在 那就返回已经存在
			echo "你已经关注他鸟";
			return;
		}

		$startTime=date("Y-m-d H:i:s");
		$sql="insert into friend(frid,frname,foid,foname,startTime)"
		 ."values('$frid','$frname','$foid','$foname','$startTime')";
	
		$res=mysql_query($sql);
		
		if(!$res)
		{
			return false;
		}
	
		return true;
	}
}



?>